﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TypewriterEffect : MonoBehaviour
{
    string[] textPlot = { "在某一个时代，文明刚起源的时候，世界上出现了两大种族，一个是人族，一个是魔族。",
    "人类崇尚光明，而魔族崇尚着黑暗。由于信仰不合，人族和魔族多次发生战争。魔法的发明也是从这时开始的。千年以后，魔族崛起。",
    "一个少年，立志成为一名魔法师，奈何上天给他开了个很大的玩笑。且看一个无法使用魔法的少年如何成为魔法师，且看一个无法使用魔法的少年是如何突破层层阻碍最终成为强者，如何带领人类消灭魔族，走向巅峰。“魔法，因我而存。魔法，因我而生。魔法，因我而在。",
    "一场大战过后，你法力尽失......\n逐渐从昏迷中醒来，努力回想着自己的名字。\n我叫什么来着......"};
    public float charsPerSecond = 0.2f;//打字时间间隔
    private string words;//保存需要显示的文字

    private bool isActive = false;
    private float timer;//计时器
    Text myText;
    private int currentPos = 0;//当前打字位置

    public GameObject[] bJ;//背景
    int bJIndex = 0;//当前第几个背景
    public GameObject inputName;//输入的姓名
    public GameObject okBtn;//确定按钮
    public GameObject randomNameBtn;//随机的姓名
    public GameObject IAm;
    public GameObject prompt;
    // Use this for initialization
    void Start()
    {
        timer = 0;
        isActive = true;
        charsPerSecond = Mathf.Max(0.2f, charsPerSecond);//返回两个值之间的最大值
        myText = GetComponent<Text>();
        words = textPlot[bJIndex];
        myText.text = "";//获取Text的文本信息，保存到words中，然后动态更新文本显示内容，实现打字机的效果
    }

    // Update is called once per frame
    void Update()
    {
        OnStartWriter();
        BJlide();
    }

    public void StartEffect()
    {
        isActive = true;
    }
    /// <summary>
    /// 执行打字任务
    /// </summary>
    void OnStartWriter()
    {

        if (isActive)
        {
            timer += Time.deltaTime;//增量时间
            if (timer >= charsPerSecond)
            {//判断计时器时间是否到达
                timer = 0;
                currentPos++;
                myText.text = words.Substring(0, currentPos);//刷新文本显示内容   Substring截取字符

                if (currentPos >= words.Length)
                {
                    OnFinish();
                }
            }

        }
    }
    /// <summary>
    /// 结束打字，初始化数据
    /// </summary>
    void OnFinish()
    {
        isActive = false;
        timer = 0;
        currentPos = 0;
        myText.text = words;
    }
    //点击屏幕任意位置
    public void ClickAnyWhere()
    {
        if (isActive)
        {
            charsPerSecond = 0.00001f;

        }
        else
        {
            if (bJIndex < 3)
            {
                bJ[bJIndex].SetActive(false);
                bJIndex += 1;
                bJ[bJIndex].SetActive(true);
                if (bJIndex == 3)
                {
                    GameObject.Find("ClickAnywhereToContinue").gameObject.SetActive(false);
                    GameObject.Find("SkipThePlot").gameObject.SetActive(false);
                    okBtn.SetActive(true);
                    IAm.gameObject.SetActive(true);
                }
                charsPerSecond = 0.2f;
                timer = 0;
                isActive = true;
                words = textPlot[bJIndex];
                myText.text = "";//获取Text的文本信息，保存到words中，然后动态更新文本显示内容，实现打字机的效果
            }
        }
    }
    //滑动的背景
    void BJlide()
    {
        switch (bJIndex)
        {
            case 0:
                if(bJ[bJIndex].transform.localPosition.x > -3500)
                {
                    bJ[bJIndex].transform.localPosition =new Vector3(bJ[bJIndex].transform.localPosition.x - 60 * Time.deltaTime, bJ[bJIndex].transform.localPosition.y, bJ[bJIndex].transform.localPosition.z);
                }
                break;
            case 1:
                if (bJ[bJIndex].transform.localPosition.x > -3600)
                {
                    bJ[bJIndex].transform.localPosition = new Vector3(bJ[bJIndex].transform.localPosition.x - 60 * Time.deltaTime, bJ[bJIndex].transform.localPosition.y, bJ[bJIndex].transform.localPosition.z);
                }
                break;
            case 2:
                if (bJ[bJIndex].transform.localPosition.y < 3000)
                {
                    bJ[bJIndex].transform.localPosition = new Vector3(bJ[bJIndex].transform.localPosition.x, bJ[bJIndex].transform.localPosition.y + 60 * Time.deltaTime, bJ[bJIndex].transform.localPosition.z);
                }
                break;
            case 3:
                if (bJ[bJIndex].transform.localPosition.x > -2252)
                {
                    bJ[bJIndex].transform.localPosition = new Vector3(bJ[bJIndex].transform.localPosition.x - 60 * Time.deltaTime, bJ[bJIndex].transform.localPosition.y, bJ[bJIndex].transform.localPosition.z);
                }
                break;
        }
    }
    //跳过剧情
    public void SkipThePlotBtn()
    {
        bJ[bJIndex].SetActive(false);
        bJIndex = 3;
        bJ[bJIndex].SetActive(true);
        okBtn.SetActive(true);
        IAm.SetActive(true);
        timer = 0;
        isActive = true;
        words = textPlot[bJIndex];
        myText.text = "";
        charsPerSecond = 0.00001f;
        GameObject.Find("ClickAnywhereToContinue").gameObject.SetActive(false);
        GameObject.Find("SkipThePlot").gameObject.SetActive(false);

        

    }

    //输入姓名,点击确定
    public void OKButton()
    {
        if (inputName.GetComponent<InputField>().text == "")
        {
            prompt.SetActive(true);
            Invoke("noprompt", 1f);
        }
        else
        {
            VariableDocument.GetInstance.name = inputName.GetComponent<InputField>().text;
            GoToInterface.ToInterface("3MainGame");
        }

    }
    void noprompt()
    {
        prompt.SetActive(false);
    }
    //随机姓名
    public void RandomName()
    {
        string temporaryName = GetChinessName();
        //Debug.Log(temporaryName);
        inputName.GetComponent<InputField>().text = temporaryName;
    }


    /// <summary>
    /// 随机获取三个字的名字
    /// </summary>
    /// <returns></returns>
    public string GetChinessName()
    {
        string name = "";
        string[] _crabofirstName = new string[]{
            "白","毕","卞","蔡","曹","岑","常","车","陈","成" ,"程","池","邓","丁","范","方","樊","闫","倪","周",
            "费","冯","符","元","袁","岳","云","曾","詹","张","章","赵","郑" ,"钟","周","邹","朱","褚","庄","卓"
           ,"傅","甘","高","葛","龚","古","关","郭","韩","何" ,"贺","洪","侯","胡","华","黄","霍","姬","简","江"
           ,"姜","蒋","金","康","柯","孔","赖","郎","乐","雷" ,"黎","李","连","廉","梁","廖","林","凌","刘","柳"
           ,"龙","卢","鲁","陆","路","吕","罗","骆","马","梅" ,"孟","莫","母","穆","倪","宁","欧","区","潘","彭"
           ,"蒲","皮","齐","戚","钱","强","秦","丘","邱","饶" ,"任","沈","盛","施","石","时","史","司徒","苏","孙"
           ,"谭","汤","唐","陶","田","童","涂","王","危","韦" ,"卫","魏","温","文","翁","巫","邬","吴","伍","武"
           ,"席","夏","萧","谢","辛","邢","徐","许","薛","严" ,"颜","杨","叶","易","殷","尤","于","余","俞","虞"
           };

        string _lastName = "震南洛栩嘉光琛潇闻鹏宇斌威汉火科技梦琪忆柳之召腾飞慕青问兰尔岚元香初夏沛菡傲珊曼文乐菱痴珊恨玉惜香寒新柔语蓉海安夜蓉涵柏水桃醉蓝春语琴从彤" +
            "傲晴语菱碧彤元霜怜梦紫寒妙彤曼易南莲紫翠雨寒易烟如萱若南寻真晓亦向珊慕灵以蕊寻雁映易雪柳孤岚笑霜海云凝天沛珊寒云冰旋宛儿" +
            "绿真盼晓霜碧凡夏菡曼香若烟半梦雅绿冰蓝灵槐平安书翠翠风香巧代云梦曼幼翠友巧听寒梦柏醉易访旋亦玉凌萱访卉怀亦笑蓝春翠靖柏夜蕾" +
            "冰夏梦松书雪乐枫念薇靖雁寻春恨山从寒忆香觅波静曼凡旋以亦念露芷蕾千帅新波代真新蕾雁玉冷卉紫千琴恨天傲芙盼山怀蝶冰山柏翠萱恨松问旋" +
            "南白易问筠如霜半芹丹珍冰彤亦寒寒雁怜云寻文乐丹翠柔谷山之瑶冰露尔珍谷雪乐萱涵菡海莲傲蕾青槐洛冬易梦惜雪宛海之柔夏青妙菡春竹痴梦紫蓝晓巧幻柏" +
            "元风冰枫访蕊南春芷蕊凡蕾凡柔安蕾天荷含玉书雅琴书瑶春雁从安夏槐念芹怀萍代曼幻珊谷丝秋翠白晴海露代荷含玉书蕾听访琴灵雁秋春雪青乐瑶含烟涵双" +
            "平蝶雅蕊傲之灵薇绿春含蕾梦蓉初丹听听蓉语芙夏彤凌瑶忆翠幻灵怜菡紫南依珊妙竹访烟怜蕾映寒友绿冰萍惜霜凌香芷蕾雁卉迎梦元柏代萱紫真千青凌寒" +
            "紫安寒安怀蕊秋荷涵雁以山凡梅盼曼翠彤谷新巧冷安千萍冰烟雅友绿南松诗云飞风寄灵书芹幼蓉以蓝笑寒忆寒秋烟芷巧水香映之醉波幻莲夜山芷卉向彤小玉幼";

        name = _crabofirstName[Random.Range(0, _crabofirstName.Length - 1)] + _lastName[Random.Range(0, _lastName.Length - 1)] + _lastName[Random.Range(0, _lastName.Length - 1)];
        return name;
    }
}


